<template>
	<div class="device-service">
		<div>
			<label>{{ $t('route.Equipment_information') }}</label>
			<div></div>
			<button><img /><label>{{ query.typeNum }}</label></button>
		</div>

		<div>
			<div>
				<div>
					<el-radio-group @change='typeChange' v-model="query.type">
						<el-radio-button class="radio-button" :label="null">{{ $t('route.All') }}</el-radio-button>
						<el-radio-button v-for="index in 12" class="radio-button" :label="index - 1">
							{{ formatTypeName(index - 1) }}
						</el-radio-button>
					</el-radio-group>
				</div>
				<div class="div-handle-right">
					<el-button v-if="user.type == 0" @click='jumpPage(0)'>{{ $t('route.Add') }}</el-button>
					<el-button @click="searchClick">{{ $t('route.search') }}</el-button>
					<input v-model="query.search" placeholder="" />
				</div>
			</div>

			<el-table class="table" :data="deviceServiceList" style="width: 100%">
				<el-table-column prop="title" :label="$t('route.title')" />
				<el-table-column prop="typeNum" :label="$t('route.type')" />
				<el-table-column :label="$t('route.Type_of_document')">
					<template #default="scope">
						{{ formatTypeName(scope.row.type) }}
					</template>
				</el-table-column>
				<el-table-column :label="$t('route.File_Type')">
					<template #default="scope">
						{{ formatSuffixName(scope.row.suffix) }}
					</template>
				</el-table-column>
				<el-table-column :label="$t('route.operate')">
					<template #default="scope">
						<el-button type="text" icon="el-icon-edit" @click="watchClick(scope.row)">
							{{ scope.row.suffix == 0 ? $t('route.Picture_view') : $t('route.Video_viewing') }}
						</el-button>
						<el-button style="color: #FF0000;" @click='deleteClick(scope.row.id)' type="text">{{
							$t('route.Delete') }}</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="pagination">
				<el-pagination style="float: right;" background @current-change="handlePageChange"
					:current-page="query.pageNum" :page-size="query.pageSize" layout="total, prev, pager, next"
					:total="query.total"></el-pagination>
			</div>

		</div>

	</div>
</template>


<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import Constant from '../../js/common/constant.js'
import i18n from '../../i18n/index';
var mTypeNum = null;
export default {
	data() {
		return {
			user: null,
			deviceServiceList: [],
			query: {
				search: null,
				typeNum: null,
				type: null,
				pageNum: 1,
				pageSize: 10,
				total: 0
			},
		};
	},
	created() {
		this.user = this.$store.state.user;
		this.query.typeNum = CommonUtils.getParam(this, 'typeNum', 'setTypeNum');
		if (mTypeNum != null) {
			this.query.typeNum = mTypeNum;
		}
		this.getDeviceServiceList();
	},
	methods: {
		deleteClick(id) {
			this.$confirm(i18n.global.t('route.Will'), i18n.global.t('route.reminder'), {
				confirmButtonText: i18n.global.t('route.sure'),
				cancelButtonText: i18n.global.t('route.cancel'),
				type: 'warning'
			}).then(() => {
				http.post(api.DELETE_DEVICE_SERVICE, {
					deviceServiceId: id,
				}, data => {
					this.$message.success(i18n.global.t('route.Delete_succeeded'));
					this.getDeviceServiceList();
				})
			});
		},
		typeChange() {
			this.getDeviceServiceList();
		},
		jumpPage() {
			var pageName = this.$store.state.user.type == 0 ? 'admin-add-device-service' :
				'customer-add-device-service';
			this.$router.push({
				name: pageName,
				params: {
					typeNum: this.query.typeNum
				}
			});
		},

		formatTypeName(type) { //0:使用文档 1:零件手册 2:常见错误 3:安装教程
			return Constant.DEVICE_SERVICE_TYPE_LIST[type];
		},
		formatSuffixName(type) { //0:图片 1:视频 2:PDF
			if (type == 0) {
				return i18n.global.t('route.Picture');
			} else if (type == 1) {
				return i18n.global.t('route.video');
			}
			return i18n.global.t('route.Picture');
		},
		getDeviceServiceList() {
			http.post(api.GET_ALL_DEVICE_SERVICE, {
				typeNum: this.query.typeNum,
				type: this.query.type,
				search: this.query.search,
				pageNum: this.query.pageNum,
				pageSize: this.query.pageSize
			}, data => {
				this.deviceServiceList = data.data.deviceServiceList;
				this.query.total = data.data.total;
			});
		},
		watchClick(item) {
			if (item.suffix == 0) {
				var pageName = this.$store.state.user.type == 0 ? 'admin-image-preview-list' :
					'customer-image-preview-list';
				this.$router.push({
					name: pageName,
					params: {
						deviceService: JSON.stringify(item),
						typeNum: this.query.typeNum
					}
				}); //页面跳转
			} else if (item.suffix == 1) {
				var pageName = this.$store.state.user.type == 0 ? 'admin-video-preview' :
					'customer-video-preview';
				this.$router.push({
					name: pageName,
					params: {
						videoUrl: item.url,
						typeNum: this.query.typeNum
					}
				}); //页面跳转
			}

		},
		searchClick() {
			this.query.pageNum = 1;
			this.query.type = null;
			this.getDeviceServiceList();
		},
		handlePageChange(page) {
			this.query.pageNum = page;
			this.getDeviceServiceList();
		},
	},
	beforeRouteEnter(to, from, next) {
		mTypeNum = from.params.typeNum;
		console.log("====typeNum=====" + from.params.typeNum);
		next(); // 必需调用
	}
};
</script>



<style lang="scss" scoped>
.device-service {
	color: #444444;
	font-size: 18px;
	padding-right: 20px;
	height: calc(100vh - 100px);
	// overflow: scroll;
	scrollbar-width: none;
	/* firefox */
	-ms-overflow-style: none;
	/* IE 10+ */
	overflow-x: hidden;
	overflow-y: auto;

	>div:nth-of-type(1) {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-top: 20px;
		background-color: #ffffff;
		padding: 20px;
		border-radius: 5px;

		>label:nth-of-type(1) {
			font-weight: 600;
		}

		>div:nth-of-type(1) {
			width: 1px;
			background-color: #cccccc;
			height: 15px;
			margin: 0px 20px;
		}

		>button:nth-of-type(1) {
			background-image: url(../../assets/img/customer-1.png);
			background-repeat: no-repeat;
			background-size: 100% 100%;
			padding: 5px 10px;
			border: 0px;
			display: flex;
			color: #ffffff;
			flex-direction: row;
			align-items: center;
			font-weight: 800;

			>img {
				margin-right: 7px;
				width: 15px;
				height: 15px;
				content: url(../../assets/img/customer-2.png);
			}
		}
	}

	>div:nth-of-type(2) {
		display: flex;
		flex-direction: column;
		margin-top: 20px;
		background-color: #ffffff;
		padding: 20px;
		border-radius: 5px;


		>div:nth-of-type(1) {
			display: flex;
			flex-direction: row;
			align-items: center;

			.div-handle-right {
				flex: 1;
				flex-direction: row-reverse;
				text-align: right;
				display: flex;
				align-items: center;

				>input:nth-of-type(1) {
					border: 1px solid #eeeeee;
					font-size: 15px;
					width: 200px;
					padding: 5px 8px;
					outline: none;
				}

				>button {
					color: #A866FF;
					margin-left: 10px;
					padding: 4px 20px;
				}
			}

			.radio-button {
				margin: 4px;
			}
		}

		.table {
			margin-top: 20px;
		}

	}

	.select {
		width: 120px;
		margin-top: 10px;
	}

	.echart {
		height: 150px;
		width: 100%;
	}

	.input {
		width: 250px;
	}


}
</style>
